<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
require_once (APPPATH.'libraries/specialmarkobject.php');
require_once (APPPATH.'libraries/productobject.php');

/**
 * Special Mark Model
 */
class SpecialMark_Model extends CI_Model
{
	private static $_instance;
	
	function __construct()
	{
		parent::__construct();
		self::$_instance =& $this;
	}
	
	public static function getInstance()
	{
		if (self::$_instance == NULL)
			self::$_instance = new SpecialMark_Model();
		return self::$_instance;
	}
	
	public function getAllSpecialMarks()
	{
		$marks = array();
		
		$data = $this->db->get(SPECIAL_MARK_TABLE);
		
		foreach ($data->result() as $row)
		{
			$marks[] = new SpecialMarkObject($row->id, $row->title, $row->showall);
		}
		
		return $marks;
	}
	
	public function getShowInAllSpecialMarks()
	{
		$marks = array();
		
		$this->db->where('showall', 1);
		$data = $this->db->get(SPECIAL_MARK_TABLE);
		
		foreach ($data->result() as $row)
		{
			$marks[] = new SpecialMarkObject($row->id, $row->title, $row->showall);
		}
		
		return $marks;
	}

	public function addProductsToMark($pIds, $markId)
	{
		foreach ($pIds as $pid)
		{
			$this->db->trans_start();
			$data = array("product_id" => $pid, "mark_id" => $markId);
			$this->db->insert(PRODUCT_MARK_TABLE, $data);
			$this->db->trans_complete();
			
			if ($this->db->trans_status() === FALSE)
				return INSERT_ERROR;
		}
		
		return ERROR_NONE;
	}
	
	public function getSpecialMarkById($id)
	{
		$mark = new SpecialMarkObject();
		
		$this->db->where('id', $id);
		$data = $this->db->get(SPECIAL_MARK_TABLE);
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$mark->setId($row->id);
			$mark->setTitle($row->title);
			$mark->setShowAll($row->showall);
		}
		
		return $mark;
	}
	
	public function getSpecialMarksByProductId($productId)
	{
		$marks = array();
		
		$query = "SELECT sp.id, sp.title, sp.showall FROM ".SPECIAL_MARK_TABLE." sp, ".PRODUCT_MARK_TABLE." pm WHERE sp.id = pm.mark_id AND pm.product_id = ?";
		$data = $this->db->query($query, array($productId));
		
		foreach ($data->result() as $row)
		{
			$marks[] = new SpecialMarkObject($row->id, $row->title, $row->showall);
		}
		
		return $marks;
	}
	
	public function insertSpecialMark($mark)
	{
		$query = "INSERT INTO ".SPECIAL_MARK_TABLE." VALUES(NULL, ?, ?)";
		
		$this->db->trans_start();
		$this->db->query($query, array($mark->getTitle(), $mark->getShowAll()));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return INSERT_ERROR;
		
		return ERROR_NONE;
	}
	
	public function updateSpecialMark($id, $mark)
	{
		$query = "UPDATE ".SPECIAL_MARK_TABLE." SET title=?, showall=? WHERE id=?";
		
		$this->db->trans_start();
		$this->db->query($query, array($mark->getTitle(), $mark->getShowAll(), $mark->getId()));
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		
		return ERROR_NONE;
	}
	
	public function deleteSpecialMarkProduct($markId, $productId)
	{
		$this->db->trans_start();
		$this->db->where('product_id', $productId);
		$this->db->where('mark_id', $markId);
		$this->db->delete(PRODUCT_MARK_TABLE);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		return ERROR_NONE;
	}
	
	public function deleteMultipleSpecialMarkProduct($markId, $productIds)
	{
		$query = "";
		$size = count($productIds);
		$query = "DELETE FROM ".PRODUCT_MARK_TABLE." WHERE product_id IN (";
		
		for($i = 0; $i < ($size - 1); $i++)
		{
			$query .= "?, ";
		}
		
		$query .= "?) AND mark_id=".$markId;
		
		$this->db->trans_start();
		$this->db->query($query, $productIds);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		return ERROR_NONE;
	}
	
	public function insertSpecialMarkProduct($markId, $productId)
	{
		$this->db->trans_start();
		$data = array('product_id' => $productId, 'mark_id' => $markId);
		$this->db->insert(PRODUCT_MARK_TABLE, $data);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return UPDATE_ERROR;
		return ERROR_NONE;
	}
	
	public function deleteSpecialMark($id)
	{
		$this->trans_start();
		$this->db->where('id', $id);
		$this->db->delete(SPECIAL_MARK_TABLE);
		$this->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		
		return ERROR_NONE;
	}
	
	public function deleteMultipleSpecialMarks($ids)
	{
		$query = "";
		$size = count($ids);
		$query = "DELETE FROM ".SPECIAL_MARK_TABLE." WHERE id IN (";
		
		for($i = 0; $i < ($size - 1); $i++)
		{
			$query .= "?, ";
		}
		
		$query .= "?)";
		
		$this->db->trans_start();
		$this->db->query($query, $ids);
		$this->db->trans_complete();
		
		if ($this->db->trans_status() === FALSE)
			return DELETE_ERROR;
		return ERROR_NONE;
	}
	
	public function isMarkExisted($id)
	{
		$this->db->where('id', $id);
		$data = $this->db->get(SPECIAL_MARK_TABLE);

		return ($data->num_rows() > 0);
	}
	
	public function findSpecialMark($title)
	{
		$id = -1;
		
		$query = "SELECT id FROM " . SPECIAL_MARK_TABLE . " WHERE title LIKE N'%" . $this->db->escape_like_str($title) . "%'";
		$data = $this->db->query($query);
		
		if ($data->num_rows() > 0)
		{
			$row = $data->row();
			$id = $row->id;
		}

		return $id;
	}
}


?>